import {TYPES} from '../reduces/registerReduces';
import ProductModel from '../../../model/catalog/category';
import _ from 'lodash';
import hash from 'object-hash';
import {Toast} from 'antd-mobile-rn';
import {
    BigNumber
} from 'bignumber.js'
let zeroNumberControl = new BigNumber(0);
let product = new ProductModel();

export function getList() {
    return (dispatch, getState) => {
        dispatch({
            type:TYPES.CHANGELOADING,
            loading:true
        })
        getIndexMenu(dispatch);
    }
}

function getIndexMenu(dispatch) {
    console.log("getIndexMenu")
    product.getCategoryProducts()
    .then((res) => {
        res = res.map((item,index) => {
            item.products.unshift({});
            item["key"] = `key_${index}`;
            return item;
        })
        dispatch({
            type:TYPES.LOADPRODUCT,
            categorys: res || []
        })
    })
    .catch(e=>{
        console.log(e);
        dispatch({
            type:TYPES.CHANGELOADING,
            loading:false
        })
    })
}

export function changeProductClassify(index){
    return (dispatch,getState)=>{
        dispatch({
            type: TYPES.CHANGEPRODUCTCLASSIFY,
            classIndex:index
        })
    }
}

export function add2cart(item){
    return (dispatch,getState)=>{
        let {cartList} = getState().register;
        let exist = cartList.find((product,index)=>{
            if(product.id == item.id  && hash(item.options) == hash(product.options)){
                if(product.id == 0 && item.name != product.name){
                    return false;
                }
                product.qty +=  item.qty;
                return true;
            }else{
                return false;
            }
        })
        if(!exist){
            cartList.push(item);
        }
        dispatch({
            type:TYPES.ADDCARTLISR,
            cartList:cartList
        })
    }
}

export function deleteCart(){
    return (dispatch)=>{
        dispatch({
            type:TYPES.ADDCARTLISR,
            cartList:[]
        })
    }
}

export function setEditCardItem(item){
    return (dispatch,getState)=>{
        dispatch({
            type:TYPES.SETEDITCARDITEM,
            cartItem:item
        })
    }
}

export function editCardItem(key,value){
    return (dispatch,getState)=>{
        let {cartItem} = getState().register;
        cartItem[key] = value;
        dispatch({
            type:TYPES.SETEDITCARDITEM,
            cartItem:cartItem
        })
    }
}

export function editCart(){
    return (dispatch,getState)=>{
        let {cartItem,cartList} = getState().register;
        for(let item of cartList){
            if(item.id == cartItem.id && hash(item) != hash(cartItem)){
                if(item.id == 0 && item.name == cartItem.item){
                    item.qty = cartItem.qty;
                    item["note"] = cartItem.note || "";
                }else if(item.id != 0){
                    item.qty = cartItem.qty;
                    item["note"] = cartItem.note || "";
                }
            }
        }
        dispatch({
            type:TYPES.ADDCARTLISR,
            cartList:cartList
        })
    }
}

export function deleteCartByItem(cartItem){
    return (dispatch,getState)=>{
        let {cartList} = getState().register;
        let nlist = cartList.filter(item=>{
            if(item.id == cartItem.id && item.name == cartItem.item){
                return true;
            }
            return false;
        })
        dispatch({
            type:TYPES.ADDCARTLISR,
            cartList:nlist
        })
    }
}

function toast(msg = ""){
    Toast.info(msg,3,null,false)
}